package net.oschina.dajiangnan.wx.process.pay.tools;

import net.oschina.dajiangnan.util.XStreamHelper;
import net.oschina.dajiangnan.wx.util.HttpsTookit;
import net.oschina.dajiangnan.wx.util.RandomUtil;
import net.oschina.dajiangnan.wx.util.Signature;
import org.apache.log4j.Logger;

import net.oschina.dajiangnan.wx.constants.Config;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.ReverseReq;
import net.oschina.dajiangnan.wx.process.pay.xmlbean.ReverseResp;


public class Reverse {

    private static Logger logger = Logger.getLogger(Reverse.class);
    
    private static final String url = "https://api.mch.weixin.qq.com/secapi/pay/reverse";
    
    
    /**
     * 撤销订单API
     * https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
     * 
     * 
     * @param transaction_id 微信的订单号，优先使用
     * @return
     * 
     *  //SUCCESS/FAIL
        String result_code;
        //错误描述
        String err_code_des;
        //错误码
        String err_code;
        //是否需要继续调用撤销，Y-需要，N-不需要
        String recall;
     */
    public static Result reverseByTransactionId(String transaction_id){
        return reverse(transaction_id,null);
    }
    
    
    
    
    /**
     * 撤销订单API
     * https://pay.weixin.qq.com/wiki/doc/api/micropay.php?chapter=9_11&index=3
     * 
     * 
     * @param out_trade_no 商户系统内部的订单号,transaction_id、out_trade_no二选一，如果同时存在优先级：transaction_id> out_trade_no
     * @return
     * 
     *  //SUCCESS/FAIL
        String result_code;
        //错误描述
        String err_code_des;
        //错误码
        String err_code;
        //是否需要继续调用撤销，Y-需要，N-不需要
        String recall;
     */
    public static Result reverseByOutTradeNo(String out_trade_no){
        return reverse(null,out_trade_no);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    private static Result reverse(String transaction_id,String out_trade_no){
        
        ReverseReq reverseReq = new ReverseReq();

        reverseReq.setAppid(Config.appid);
        reverseReq.setMch_id(Config.mch_id);
        if(transaction_id != null){
            reverseReq.setTransaction_id(transaction_id);
        }
        if(out_trade_no != null){
            reverseReq.setOut_trade_no(out_trade_no);
        }
        reverseReq.setNonce_str(RandomUtil.getRandomStr(32));
        
        try {
            reverseReq.setSign(Signature.getSign(reverseReq, null, Config.apikey));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        
        logger.debug("撤销订单请求参数如下\r\n" + reverseReq);
        String xmlStr;
        try {
            xmlStr = new HttpsTookit(Config.pkcs12FileName,Config.pics_pass).doPost(url,reverseReq.toString(),null,null,"UTF-8");
        } catch (Exception e) {
            logger.error(e);
            e.printStackTrace();
            return null;
        }
        ReverseResp reverseResp = XStreamHelper.toBean(xmlStr, ReverseResp.class);
        logger.debug("撤销订单应答内容如下\r\n" + reverseResp);
        

        if(!reverseResp.islegalSign(Config.apikey)){
            Result result = new Result();
            result.setMsg("MicropayResp UnlegalSign");
            return result;
        }
        
        if(reverseResp.getReturn_code().equals("SUCCESS")){
            Result result = new Result();
            result.setReverseResp(reverseResp);
            return result;
        }else{
            return null;
        }
    }
    
    
    public static class Result{
        private String msg;
        private ReverseResp reverseResp;
        
        public String getMsg() {
        
            return msg;
        }
        
        public void setMsg(String msg) {
        
            this.msg = msg;
        }
        
        public ReverseResp getReverseResp() {
        
            return reverseResp;
        }
        
        public void setReverseResp(ReverseResp reverseResp) {
        
            this.reverseResp = reverseResp;
        }

    }
    
}
